/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Design/IDVSHAPRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace DVSHAP
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64 SFACTS_POS = 0;  ///< Start position in dataset SFACTS
        SG::DataStructure::Common::I64 SFACTS_LEN = 0;  ///< Number of rows in dataset SFACTS
        SG::DataStructure::Common::Id  DOMAIN_ID  = 0;  ///<
    };
};  // namespace DVSHAP
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::DVSHAP::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "SFACTS_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "SFACTS_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::DVSHAPData::SFACTSData> ()
{
    HighFive::CompoundType ct = { { "DVID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "COLID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "SF", HighFive::create_datatype<SG::DataStructure::Common::Real> () } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class DVSHAPRepository : public IDVSHAPRepository, public RepositoryBase
{
    using MainStruct      = SG::DataStructure::FEM::DVSHAPData;
    using SubStructSFACTS = SG::DataStructure::FEM::DVSHAPData::SFACTSData;

    using NasMainStruct = SG::DBManager::HDF5::DVSHAP::IDENTITY;

  public:
    explicit DVSHAPRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_SFACTS
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /// @brief 写入配置
    /// @param entity
    virtual SG::DataStructure::Common::Status Set (const SG::DataStructure::FEM::DVSHAPData& entity) override;

    /// @brief 获取配置
    /// @return
    virtual SG::DataStructure::FEM::DVSHAPData Get () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (const MainStruct&                  OwnIdent,
                       NasMainStruct& _OUT                IDENTITYData,
                       std::vector<SubStructSFACTS>& _OUT subDataSFACTS,
                       bool                               isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (NasMainStruct& IDENTITYData, std::vector<SubStructSFACTS>& subDataSFACTS, MainStruct& _OUT OwnIdent);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
